﻿using System.Collections.Generic;
using System.IO;
using BibtexParser.Models;

namespace BibtexParser
{
    public class Parser
    {
        // Based on BibtexParser for Java by Mikkel Kjær Jensen and Simon F. Lund
        // source: http://sourceforge.net/projects/bibregexparser/

        public List<Article> ParseBibtexFile(Stream bibtexFileStream)
        {
        #region Regex patterns
        // The pattern that will be used to find the individual bibtexs entries

        var bibtexPublications = 
            // pub type (Article, InProceedings etc.)
            "@([^{]+)\\{\\s*([^,=]+),"
            // The rest of the entry
            + "((?:(?:\\s+(?:[\\w]+)\\s+=\\s)[^\n]+)+)";

        // The main pattern, the one which are going to extract most of the information from the post	
        var mainPat = 
            // First of all we have the left hand side where we first have the category, then some spaces, a equals sign and then some more spaces. 
            "\\s*(\\w+)\\s*=\\s*"
                // and then we have some stuff
            + "([\\w\\W]+),?";

        //	Extract abbreviations     SHORTHAND    =    EXPANDED                  
        var bibtexAbbPat = "@string\\{\\s*"
                                              + "([^\\s]+)"
                                              + "(?>\\s*=\\s*\")"
                                              + "([^\"]+)"
                                              + "\"\\s*\\}";
        var preamblePat = "@preamble\\s*(?:\\{|\\()" +
                                              "(\\[\\w\\W]*)(?:\\}|\\))";

        var author = 
            //	If there is a comma, then this is the first part 
            "(?:\"?(?:([^,]+)," +
            // 	Otherwise this is the second	
            "|([^\\s]+))" +
            // Any amount (larger than zero) amount of spaces
            "(?>\\s+" +
            // The second part of the name
            "([^\\s]+(?:\\s[^\\s]+)*)?\"?\\s*)?)";

        var cleanBracketStart = "(^\\s*(?:\\{)+\\s*)";
        var cleanBracketEnd = "((?:\\})+\\s*,?\\s*)$";
        var cleanQouteStart = "(^\\s*(?:\")+)";
        var cleanQouteEnd = "((?:\")\\s*,?\\s*)$";
        #endregion
        #region Matchers

            System.Text.RegularExpressions.Match =;
        #endregion
        #region Privates
        var expandStrings = new List<string>();
        Dictionary<string, string> log = new Dictionary<string, string>();
        #endregion
            var sr = new StreamReader(bibtexFileStream);
            string input;
            while (sr.Peek() >= 0)
            {
                input = sr.Read
            }
        } 
    }
}